001 /* $RCSfile: ByteUtil.java,v $ 002 * $Revision: 1.2 $ 003 * $Date: 2002/01/04 14:05:40 $ 004 * $Author: uwe $ 005 * $State: Exp $ 006 * 007 * Created on July 12, 2001, 5:20 PM 008 * 009 * Copyright (C) 2001 Uwe Guenther <uwe@cscc.de> 010 * 011 * This file is part of the jhbci JCE-ServiceProvider. The jhbci JCE- 012 * ServiceProvider is a library, written in JavaTM, that should be 013 * used in HBCI banking applications (clients and may be servers), 014 * to do cryptographic operations. 015 * 016 * The jhbci library is free software; you can redistribute it and/or 017 * modify it under the terms of the GNU Lesser General Public 018 * License as published by the Free Software Foundation; either 019 * version 2.1 of the License, or (at your option) any later version. 020 * 021 * The jhbci library is distributed in the hope that it will be useful, 022 * but WITHOUT ANY WARRANTY; without even the implied warranty of 023 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 024 * Lesser General Public License for more details. 025 * 026 * You should have received a copy of the GNU Lesser General Public 027 * License along with this library; if not, write to the Free Software 028 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 029 * 030 */ 031 032 package de.cscc.crypto.util; 033 034 import javax.crypto.IllegalBlockSizeException; 035 036 /** 037 * ByteUtil Class. 038 * 039 * Class that holds only static methods to convert bytes and byte arrays to 040 * Strings. These Strings holds the hex or binary representation of the 041 * converted byte or byte arrays. This class can't be instance, because 042 * its constructor is private. 043 * 044 * @author <a href=mailto:uwe@cscc.de>Uwe Günther</a> 045 * @version $Revision: 1.2 $ 046 */ 047 public final class ByteUtil { 048 049 /** This class can't be instance, because its constructor is private.*/ 050 private ByteUtil() {} 051 052 /** 053 * Converts byte to binary representation. 054 * 055 * @param in byte that will be converted. 056 * @return String with hex representation of in. 057 */ 058 public static String toBin(int in){ 059 060 return "0b" + toBinGeneric(in); 061 } 062 063 /** 064 * Converts byte[] to binary representation. 065 * 066 * @param in byte[] that will be converted. 067 * @return String with hex representation of in. 068 */ 069 public static String toBin(byte[] in){ 070 071 StringBuffer tmp = new StringBuffer(); 072 073 for (int i = 0; i < in.length; i++) { 074 tmp.append(toBin(in[i]) + " "); 075 } 076 077 return "0b " + tmp.toString(); 078 } 079 080 /** 081 * Converts byte to binary representation. 082 * 083 * @param in byte that will be converted. 084 * @return String with hex representation of in. 085 */ 086 private static String toBinGeneric(int in){ 087 088 StringBuffer tmp = new StringBuffer(); 089 090 for(int i = 0; i < 8; i++){ 091 tmp.insert(0, ((in >> i) & 0x01) ); 092 } 093 094 return tmp.toString(); 095 } 096 097 /** 098 * Converts byte to hex representation. 099 * 100 * @param in byte that will be converted. 101 * @return String with hex representation of in. 102 */ 103 public static String toHex(byte in) { 104 105 return "0x" + toHexGeneric(in); 106 } 107 108 /** 109 * Converts byte[] to hex representation. 110 * 111 * @param in byte[] that will be converted. 112 * @return String with hex representation of in. 113 */ 114 public static String toHex(byte[] in) { 115 116 StringBuffer tmp = new StringBuffer("0x"); 117 118 for (int i = 0; i < in.length; i++) { 119 tmp.append(toHexGeneric(in[i])); 120 } 121 122 return tmp.toString(); 123 } 124 125 /** 126 * Converts byte to binary representation. 127 * 128 * @param in byte that will be converted. 129 * @return String with hex representation of in. 130 */ 131 private static String toHexGeneric(byte in){ 132 133 String tmp = ""; 134 135 byte b = (byte) (in & 0x000000f0); 136 137 switch (b){ 138 139 case 0x00: 140 tmp += "0"; 141 break; 142 143 case 0x10: 144 tmp += "1"; 145 break; 146 147 case 0x20: 148 tmp += "2"; 149 break; 150 151 case 0x30: 152 tmp += "3"; 153 break; 154 155 case 0x40: 156 tmp += "4"; 157 break; 158 159 case 0x50: 160 tmp += "5"; 161 break; 162 163 case 0x60: 164 tmp += "6"; 165 break; 166 167 case 0x70: 168 tmp += "7"; 169 break; 170 171 case (byte)0x80: 172 tmp += "8"; 173 break; 174 175 case (byte)0x90: 176 tmp += "9"; 177 break; 178 179 case (byte)0xa0: 180 tmp += "a"; 181 break; 182 183 case (byte)0xb0: 184 tmp += "b"; 185 break; 186 187 case (byte)0xc0: 188 tmp += "c"; 189 break; 190 191 case (byte)0xd0: 192 tmp += "d"; 193 break; 194 195 case (byte)0xe0: 196 tmp += "e"; 197 break; 198 199 case (byte)0xf0: 200 tmp += "f"; 201 break; 202 203 } 204 205 b = (byte)(in & 0x0000000f); 206 207 switch (b){ 208 209 case 0x00: 210 tmp += "0"; 211 break; 212 213 case 0x01: 214 tmp += "1"; 215 break; 216 217 case 0x02: 218 tmp += "2"; 219 break; 220 221 case 0x03: 222 tmp += "3"; 223 break; 224 225 case 0x04: 226 tmp += "4"; 227 break; 228 229 case 0x05: 230 tmp += "5"; 231 break; 232 233 case 0x06: 234 tmp += "6"; 235 break; 236 237 case 0x07: 238 tmp += "7"; 239 break; 240 241 case 0x08: 242 tmp += "8"; 243 break; 244 245 case 0x09: 246 tmp += "9"; 247 break; 248 249 case 0x0a: 250 tmp += "a"; 251 break; 252 253 case 0x0b: 254 tmp += "b"; 255 break; 256 257 case 0x0c: 258 tmp += "c"; 259 break; 260 261 case 0x0d: 262 tmp += "d"; 263 break; 264 265 case 0x0e: 266 tmp += "e"; 267 break; 268 269 case 0x0f: 270 tmp += "f"; 271 break; 272 273 } 274 275 return tmp; 276 } 277 278 /** 279 * Converts byte[8] to long representation. 280 * 281 * @param input byte[8] that will be converted. 282 * @param inputOffset the offset in input where input starts. 283 * @throws IllegalBlockSizeException if input length lesser than 8. 284 * @return the long representation. 285 */ 286 public static long toLong(byte[] input, int inputOffset) 287 throws IllegalBlockSizeException 288 { 289 if (input.length-inputOffset < 8){ 290 throw new IllegalBlockSizeException( 291 "Usable byte range is " + (input.length-inputOffset) + 292 " bytes large, but it should be 8 bytes or larger."); 293 } 294 295 296 long returnValue =0L; 297 298 for (int i =inputOffset; i-inputOffset < 8; i++){ 299 returnValue |= 300 (input[i] & 0x00000000000000ffL ) << (64-8 - 8*(i-inputOffset)); 301 } 302 303 return returnValue; 304 305 } 306 }